package xlx.com.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import xlx.com.myapplication.R;
import xlx.com.myapplication.bean.DataCallBack;
import xlx.com.myapplication.widget.FlowTagLayout;
import xlx.com.myapplication.widget.OnTagClickListener;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<DataCallBack> mDataSet;
    private Context mContext;
    private OnTagClickListener onTagClickListener;

    public OnTagClickListener getOnTagClickListener() {
        return onTagClickListener;
    }

    public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
        this.onTagClickListener = onTagClickListener;
    }

    //构造器，接受数据集
    public MyAdapter(Context context, List<DataCallBack> data) {
        this.mDataSet = data;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //加载布局文件
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlayout, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //将数据填充到具体的view中
        holder.mTextView.setText(mDataSet.get(position).getTitle());

        if (mDataSet.get(position).getInfo() != null && mDataSet.get(position).getInfo().size() > 0) {
            TagAdapter mColorTagAdapter = new TagAdapter(mContext);
            holder.mGrid.setAdapter(mColorTagAdapter);
            mColorTagAdapter.addDatas(mDataSet.get(position).getInfo());
            holder.mGrid.setOnTagClickListener(new OnTagClickListener() {
                @Override
                public void onItemClick(FlowTagLayout parent, View view, int id) {
                    Log.i("@@@","position-->"+position+"--id-->"+id);
                    Log.i("@@@","title-->"+mDataSet.get(position).getTitle()+"--Info()-->"+mDataSet.get(position).getInfo().get(id).getInfo());
                    Toast.makeText(mContext,""+mDataSet.get(position).getInfo().get(id).getInfo(),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public FlowTagLayout mGrid;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.num);
            mGrid = (FlowTagLayout) itemView.findViewById(R.id.flow_layout);
        }
    }
}